<style lang="sass" scoped>
    .system{
        li{
            display:inline-block;
        }
    }
    .system li a:hover{color:#fff!important;}
    .childrenlen_item{
        position:relative;
        .user-post{
            position:absolute;
            z-index: 10;
            top:0;
            left:-153px;
            background: rgba(255,255,255,0.9);
            width: 150px;
            box-shadow: 0 0 3px 0 #ccc;
            padding:5px 0;
            li{
                width: 100%;
                text-align: center;
            }
        }
        em{
            font-size:12px;
            background: #eb9316;
            color:#fff;
            padding:3px 5px;
            cursor:pointer;
            display:none;
        }
    }
    .children{
        .del-dept{
            display: none;
        }
    }
    .children:hover{
        .del-dept{
            display: inline-block;
        }
    }
    .caozuo-icon{
        .allot{
            display: none;
        }
    }
    .caozuo-icon:hover{
        .allot{
            display: inline;
        }
    }
    .choice-dept{
        position: relative;
        white-space: nowrap;
        &>span{
            overflow-x:hidden;
            display: inline-block;
            width: 175px;
        }
        .dept-box{
            border: 1px solid #cf2020;
            padding: 5px 8px;
            margin-top: 8px;
            position: absolute;
            background: #fffff5;
            .post-box{
                cursor:pointer;
                text-align: left;
                position:relative;
                .first-group{
                    display: block;
                    width: 180px;
                    height: 30px;
                    line-height: 30px;
                    padding-left:8px;
                }
                .first-list{
                    position: absolute;
                    z-index: 1;
                    width: 280px;
                    height: 30px;
                    top:0;
                    display: none;
                    div{
                        width: 180px;
                        height: 30px;
                        line-height: 30px;
                        padding-left:8px;
                    }
                    .post-group{
                        background: #fff;
                        color: #000;
                        left: 100px;
                        top: 0;
                        left: 155px;
                        width: 180px;
                        text-align: left;
                        text-indent: 30px;
                        position: absolute;
                        box-shadow: 1px 1px 3px 0 #ccc;
                        z-index: -1;
                        li{
                            width: 180px;
                            height: 30px;
                            line-height: 30px;
                            overflow: hidden;
                            white-space: nowrap;
                            text-overflow: ellipsis;
                        }
                    }
                }

            }
            .post-box:hover{
                .first-list{
                    display: block;
                    div{
                        background:#fff;
                    }
                }
            }
        }
    }
</style>
<template>
    <div class="container-fluid" v-cloak>
        <div class="row comm_conent_padding">
            <!-- 账户列表 -->
            <div class="common_block">
                <div class="head head-default">
                    <i class="largeiconfont">&#xe625;</i>
                    <span>组织管理</span>
                </div>
                <!-- 列表详情 -->
                <div class="content pd15">
                    <div class="mb20">
                        <div class="float-right">
                        </div>
                    </div>
                    <div class="top-search mb20">
                        <el-input @keyup.enter="searchUser()" v-model="userName" type="text" class="inline-block form-control input-sm w-200" />
                        <el-button @click="searchUser()" class="btn btn-sm btn-primary">搜索</el-button>
                    </div>
                    <div class="common_select_buttons">
                        <el-button class="btn btn-danger" type="danger">部门列表</el-button>
                        <el-button @click="addUserShow()" class="btn btn-default" type="button">新增用户</el-button>
                        <el-button @click="deptShow('add')" type="primary">新增部门</el-button>
                        <el-button @click="addPostShow()" class="btn btn-default" type="button">新增职位</el-button>
                        <ul class="select">
                            <li class="children" v-for="(item,index) in dataList">
                                <i class="line-heng"></i>
                                <span class="bigiconfont lightcolor cursor userSelect first_span" @click="nextSelect($event,index,item.deptId)">&#xe65f;</span>
                                <el-button class="btn btn-primary btn-xs" type="primary"><span v-text="item.deptName"></span><span
                                        class="iconfont ml5 userSelect" @click="deptShow('edit',item)">&#xe61d;</span></el-button>
                                <el-button @click="delDept(item)" class="btn btn-danger btn-xs del-dept" type="danger">删除部门</el-button>
                                <span class="add">
                                    <input type="text" v-model="causezhiwei" class="w-100 add-input pdl10 tiniestsize"
                                        placeholder="新增职位">
                                    <el-button class="btn btn-default btn-xs" @click="addDepartmentAjax(item)">新增</el-button>
                                </span>
                                <!-- 二级职位列表 -->
                                <ul class="children_ul">
                                    <li class="childrenlen" v-for="(item1,index1) in item.positionList">
                                        <i class="line-heng"></i>
                                        <span class="bigiconfont lightcolor cursor userSelect first_span" @click="nextSelect2($event,index,index1,item.deptId,item1.postId)">&#xe65f;</span>
                                        <div class="inline-block childrenlen_item caozuo-icon">
                                            <el-button class="btn btn-success btn-xs" type="success" size="small">
                                                <span v-text="item1.postName"></span>
                                                <!-- <span class="iconfont" @click="addDepartment('updatezhiwei',item1)">&#xe61d;</span> -->
                                            </el-button>
                                            <el-button @click="postAddUserShow(item,item1)" class="btn btn-primary btn-xs allot"
                                                size="small">添加用户</el-button>
                                            <el-button class="btn btn-warning btn-xs allot" @click="routerGo('dept_authority',{postId:item1.postId})"
                                                size="small">分配权限</el-button>
                                            <el-button @click="delPost(item,item1)" class="btn btn-danger btn-xs allot"
                                                type="danger" size="small">删除职位</el-button>
                                        </div>
                                        <!-- 三级用户列表 -->
                                        <ul class="children_ul">
                                            <li class="childrenlen" v-for="(item2,index2) in item1.positionList">
                                                <i class="line-heng"></i>
                                                <div @mouseenter="getUserDeptAndPostList(item2)" @mouseleave="closePostList(item2)"
                                                    class="inline-block childrenlen_item caozuo-icon">
                                                    <!-- 显示用户权限 -->
                                                    <ul v-show="item2.show" class="user-post">
                                                        <li :class="{'mt5':index3!=0}" v-for="(item3,index3) in item2.postList">{{item3.deptName}}-{{item3.postName}}</li>
                                                    </ul>
                                                    <el-button class="btn btn-info btn-xs" type="info" size="mini">
                                                        <span v-text="item2.userName"></span>
                                                    </el-button>
                                                    <el-button @click="updataPostShow(item2)" class="btn btn-xs btn-warning allot"
                                                        size="mini">设置职位</el-button>
                                                    <el-button @click="delUser(item,item1,item2)" class="btn btn-xs btn-danger allot"
                                                        type="danger" size="mini">删除用户</el-button>
                                                    <!-- <el-button @click="copyUserShow(item2)" class="btn btn-xs btn-primary allot"
                                                        size="mini">权限复制</el-button> -->
                                                </div>
                                            </li>
                                        </ul>
                                    </li>
                                </ul>
                            </li>

                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <el-dialog title="新增部门" :visible.sync="deptObj.show" width="50%">
            <div>
                <el-form label-width="140px" class="demo-ruleForm">
                    <el-form-item label="部门名称：" prop="deptName">
                        <el-input v-model.trim="deptObj.deptName"></el-input>
                    </el-form-item>
                    <el-form-item label="" prop="deptName">
                        <el-button @click="addOrUpdateDept()" type="primary">确认</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>

        <el-dialog title="新增职位" :visible.sync="addPostObj.show" width="50%">
            <div>
                <el-form label-width="140px" class="demo-ruleForm">
                    <el-form-item label="请选择部门：" prop="deptName">
                        <el-select v-model.trim="addPostObj.deptId">
                            <el-option v-for="(item,i) in dept" :key="item.deptId" :label="item.deptName" :value="item.deptId"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="职位名称：" prop="postName">
                        <el-input v-model="addPostObj.postName" type="text"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="addPostSend()" type="primary">确认</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>

        <el-dialog title="修改用户职位" :visible.sync="editPostObj.show" width="50%">
            <div>
                <el-form label-width="140px" class="demo-ruleForm">
                    <el-form-item label="真实姓名：" prop="updataName">{{editPostObj.updataName}}
                    </el-form-item>
                    <el-form-item label="账号名称：" prop="updataAccount">{{editPostObj.updataAccount}}
                    </el-form-item>
                    <el-form-item label="部门职位：" prop="deptName">
                        <el-cascader :options="deptSelect" v-model="editPostObj.choiceDept" @change="deptSelectChangeEdit">
                        </el-cascader>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="editPostSend()" type="primary">确认</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>

        <el-dialog title="新增用户" :visible.sync="addUser.show" width="50%">
            <div>
                <el-form label-width="140px" class="demo-ruleForm">
                    <el-form-item label="真实姓名：" prop="userName">
                        <el-input v-model="addUser.userName" type="text"></el-input>
                    </el-form-item>
                    <el-form-item label="账号名称：" prop="account">
                        <el-input v-model="addUser.account" type="text"></el-input>
                    </el-form-item>
                    <el-form-item label="密码：" prop="password">
                        <el-input v-model="addUser.password" type="text"></el-input>
                    </el-form-item>
                    <el-form-item label="部门职位：">
                        <el-cascader :options="deptSelect" v-model="addUser.choiceDept" @change="deptSelectChange">
                        </el-cascader>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="addUserSend()" type="primary">确认</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>

        <el-dialog title="当前职位添加用户" :visible.sync="postAddUser.show" width="50%">
            <div>
                <el-form label-width="140px" class="demo-ruleForm">
                    <el-form-item label="职位：" prop="postName">
                        <el-input v-model="postAddUser.postName" type="text" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="姓名：" prop="uid">
                        <el-select v-model="postAddUser.uid" filterable placeholder="请选择">
                            <el-option v-for="(item,i) in postAddUser.userList" :key="item.uid" :label="item.userName" :value="item.uid">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    
                    <el-form-item>
                        <el-button @click="postAddUserPost()"  type="primary">确认</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>
        
    </div>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
                //传递HTML模板弹窗参数（slot）
                deptObj: {
                    show: false,
                    deptName: '',
                    deptId: '',
                },
                bumenType: 'add', //部门表单类型
                //控制HTML弹窗显示、隐藏
                showPagepop: false,
                causeofreezing: '', //部门名称
                deptId: '', //需要修改的部门id
                postId: '',//修改的职位ID
                dataList: [],
                causezhiwei: '', //新增职位
                userName: '', //搜索用的用户名
                //新增用户
                // addUser: {
                //     show: false,
                //     account: "",
                //     choiceDept: false,
                //     deptAndPost: [],
                //     postStr: '请选择部门',
                //     password: "",
                //     userName: "",
                //     // 以下是设置职位的参数
                //     updataShow: false,
                //     updataName: '',
                //     updataAccount: '',
                //     uid: '',
                // },
                addUser:{
                    show:false,
                    userName:'',
                    account:'',
                    password:'',
                    deptJson:[],
                    choiceDept:[],
                },
                //新增职位
                addPostObj: {
                    show: false,
                    deptId: '',
                    postName: '',
                },
                editPostObj: {
                    show: false,
                    updataName:'',
                    updataAccount:'',
                },
                dept: [],//所有部门跟职位
                //给职位添加用户
                postAddUser: {
                    show: false,
                    uid: '',
                    deptId: '',
                    postId: '',
                    postName: '',
                    userList:[],
                },
                timeout: null,//定时器
                copyUserObj: {//用户权限复制
                    show: false,
                    copyObjUid: '',//源对象
                    copyObjName: '',//源对象
                    beCopyObjUid: '',//目标对象
                    userInput: '',//搜索用
                    userList: [],
                },
                editPostObj:{

                }
            };
        },
        computed: {
            'deptSelect'() {
                let list = JSON.parse(JSON.stringify(this.dept));
                let arr = [];
                list.forEach((item, i) => {
                    let obj = {
                        value: item.deptName + '-' + item.deptId,
                        label: item.deptName,
                        children: []
                    }
                    if(item.posts.length>0){
                        item.posts.forEach((item2, i2)=>{
                            obj.children.push({
                                value: item2.postName + '-' + item2.postId,
                                label: item2.postName,
                            })
                        })
                    }
                    arr.push(obj)
                })
                return arr;
            }
        },
        watch: {
            'copyUserObj.userInput'(val) {
                if (val == '') {
                    this.copyUserObj.beCopyObjUid = ''
                }
            }
        },
        created() {
            this.getInitData();
        },
        mounted() {
            this.$nextTick(() => {
                // $('body').on('click', ($event) => {
                //     if (!$($event.target).parents('.choice-dept').length) {
                //         this.addUser.choiceDept = false
                //     }
                // })
            });
        },
        methods: {
            // 初始化数据 获得所有部门信息
            getInitData() {
                var This = this;
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getAllDept',
                    success: function (data) {
                        if (data.data.length === 0) return;
                        var datas = util.pushKeVaInArr({
                            arr: data.data,
                            pushps: [
                                { key: 'positionList', val: [] },
                            ],
                        });
                        This.dataList = datas
                        This.getPostByDeptId(0, datas[0]['deptId'])
                        This.getDeptAndPost();
                    }
                })
            },
            // 搜索员工
            searchUser() {
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/searchUser',
                    data: {
                        userName: this.userName
                    },
                    success: (data => {
                        if (data.data.length == 0) {
                            popup.miss({ content: '无该用户信息' });
                            return;
                        }
                        data.data.forEach((item, i) => {
                            var index = util.isInArrayAndIndex(this.dataList, item.deptId, 'deptId').index;
                            var el = $('.select>li:nth-child(' + (index + 1) + ')>span')[0];
                            this.nextSelect(el, index, item.deptId, () => {
                                this.$nextTick(() => {
                                    var index2 = util.isInArrayAndIndex(this.dataList[index].positionList, item.postId, 'postId').index;
                                    var el2 = $('.select>li:nth-child(' + (index + 1) + ')>ul>li:nth-child(' + (index2 + 1) + ')>span')[0];
                                    this.nextSelect2(el2, index, index2, item.deptId, item.postId)
                                })
                            })
                        })
                    })
                })
            },
            // 获得某个部门下的所有职位
            getPostByDeptId(index, deptId, fn) {
                var This = this;
                if (This.dataList[index]['positionList'].length) {
                    fn && fn(); return false;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getPostByDeptId?deptId=' + deptId,
                    success: function (data) {
                        var datas = util.pushKeVaInArr({
                            arr: data.data,
                            pushps: [
                                { key: 'positionList', val: [] },
                            ],
                        })
                        This.dataList[index]['positionList'] = datas
                        fn && fn()
                    }
                })
            },
            //获取职位下的员工
            getUserByPostId(index, index2, deptId, postId, fn) {
                if (this.dataList[index]['positionList'][index2]['positionList'].length) {
                    fn && fn(); return;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getUserByPostId',
                    data: {
                        deptId: deptId, //  是   int 部门ID
                        postId: postId //  是   int 职位ID
                    },
                    success: (data => {
                        data.data.forEach((item, i) => {
                            //show标示是否显示用户权限
                            item['show'] = false;
                            //postList为用户权限列表
                            item['postList'] = [];
                        })
                        this.dataList[index]['positionList'][index2]['positionList'] = data.data;
                        fn && fn()
                    })
                })
            },
            // 加载部门及职位
            getDeptAndPost() {
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getAllDeptAndPost',
                    nohideloading: true,
                    type: 'post',
                    success: (data => {
                        data.data.forEach((item, i) => {
                            item.posts.forEach((item2, i2) => {
                                item2['isChecked'] = false;
                            })
                        })
                        this.dept = data.data;
                    })
                })
            },
            // 新增用户
            // addUserShow() {
            //     this.addUser.show = true;
            //     this.addUser.account = "";
            //     this.addUser.choiceDept = false;
            //     this.addUser.deptAndPost = [];
            //     this.addUser.postStr = '请选择部门';
            //     this.addUser.password = "";
            //     this.addUser.userName = "";
            //     this.dept.forEach((item, i) => {
            //         item.posts.forEach((item2, i2) => {
            //             item2.isChecked = false;
            //         })
            //     })
            // },
            addUserShow(){
                this.addUser={
                    show: true,
                    userName: '',
                    account: '',
                    password: '',
                    deptJson: [],
                    choiceDept: [],
                }
            },
            //选择部门及其职位
            choicePost(item, item2) {
                var data = {
                    deptId: item.deptId,
                    deptName: item.deptName,
                    postId: item2.postId,
                    postName: item2.postName,
                };
                var check = util.isInArrayAndIndex(this.addUser.deptAndPost, item2.postId, 'postId')
                if (check.isin) {//如果选择的职位已经存在，则去掉选择，如果不存在，就push进addUser.deptAndPost
                    item2.isChecked = false;
                    this.addUser.deptAndPost.splice(check.index, 1)
                } else {
                    item2.isChecked = true;
                    this.addUser.deptAndPost.push(data)
                }
                if (this.addUser.deptAndPost.length == 0) {
                    this.addUser.postStr = '请选择部门'
                } else {
                    this.addUser.postStr = util.getArrsItems(this.addUser.deptAndPost, 'postName')
                }
            },
            //新建用户提交
            addUserSend() {
                // if (!util.regCombination('c', [2, 20]).test(this.addUser.userName.trim())) {
                //     popup.alert({ type: 'error', content: '请输入正确的真实姓名' });
                // } else if (!util.regCombination('*', [4, 16]).test(this.addUser.account.trim())) {
                //     popup.alert({ type: 'error', content: '请输入4-16位账号名称' });
                // } else if (!util.regCombination('*', [6, 16]).test(this.addUser.password.trim())) {
                //     popup.alert({ type: 'error', content: '请输入6-16位密码' });
                // } else if (this.addUser.deptAndPost.length == 0) {
                //     popup.alert({ type: 'error', content: '请选择部门' });
                // } else {
                    util.ajax({
                        url: config.baseApi + 'bookMall-admin/api/admin/addUser',
                        type: 'post',
                        data: {
                            account: this.addUser.account.trim(),
                            password: this.addUser.password.trim(),
                            userName: this.addUser.userName.trim(),
                            deptJson: JSON.stringify(this.addUser.deptJson)
                        },
                        success: (data => {
                            popup.miss({ type: 'msg', content: '新建成功' });
                            this.addUser.show = false;
                            this.getInitData();
                            this.dept.forEach((item, i) => {
                                item.posts.forEach((item2, i2) => {
                                    item2['isChecked'] = false;
                                })
                            })
                        })
                    })
                // }
            },
            //新增职位展示
            addPostShow() {
                this.dept.forEach((item, i) => {
                    item.posts.forEach((item2, i2) => {
                        item2.isChecked = false;
                    })
                })
                this.addPostObj = {
                    deptId:'',
                    postName:'',
                    show:true,
                }
            },
            //新增职位
            addPostSend() {
                if (this.addPostObj.deptId == '') {
                    popup.alert({ content: '请选择部门!' }); return;
                }
                if (!util.regCombination('*').test(this.addPostObj.postName)) {
                    popup.alert({ content: '请填写需要新增的职位!' }); return;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/addPost',
                    data: {
                        deptId: this.addPostObj.deptId,
                        postName: this.addPostObj.postName
                    },
                    success: (data => {
                        this.getInitData();
                        this.addPostObj.show = false;
                        popup.miss({ content: '操作成功!' })
                    })
                })
            },
            // 设置职位展示
            // updataPostShow(item) {
            //     //根据用户名称查询该用户的已有权限，并打钩
            //     util.ajax({
            //         url: config.baseApi + 'bookMall-admin/api/admin/searchUser',
            //         data: {
            //             userName: item.userName
            //         },
            //         success: (data => {
            //             this.addUser.postStr = '';
            //             this.addUser.deptAndPost = [];
            //             this.addUser.updataName = item.userName;
            //             this.addUser.updataAccount = item.account;
            //             this.addUser.uid = item.uid;
            //             this.dept.forEach((item, i) => {
            //                 item.posts.forEach((item2, i2) => {
            //                     item2.isChecked = false;
            //                 })
            //             })
            //             var str = ''
            //             data.data.forEach((item, i) => {//第一次是循环所有的权限
            //                 this.dept.forEach((item2, i2) => {//第二次是循环dept，再循环post，找到里面的ischecked打钩
            //                     item2.posts.forEach((item3, i3) => {
            //                         if (item3.postId == item.postId) {
            //                             item3.isChecked = true;
            //                             str += item3.postName + ','
            //                             this.addUser.deptAndPost.push(item3)
            //                         }
            //                     })
            //                 })
            //             })
            //             this.addUser.postStr = str.slice(0, -1)
            //             //结束循环
            //             this.addUser.updataShow = true;
            //         })
            //     })

            // },
            updataPostShow(item){
                //根据用户名称查询该用户的已有权限，并打钩
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/searchUser',
                    data: {
                        userName: item.userName
                    },
                    success: (data => {

                    })
                })
                this.editPostObj= {
                    show: true,
                    updataName: item.userName,
                    updataAccount: item.account,
                }
            },
            // 设置职位方法
            updataPost() {
                if (this.addUser.deptAndPost.length == 0) {
                    popup.alert({ content: '请至少设置一个权限' }); return;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/modifyAuthority',
                    data: {
                        uid: this.addUser.uid,  // 是   int 用户ID
                        authorityListStr: JSON.stringify(this.addUser.deptAndPost),  //    是   int 职位列表：格式为：[{"deptId":1,"postId":2}]
                    },
                    success: (data => {
                        popup.miss({ content: '设置成功！' });
                        this.getInitData();
                        this.addUser.updataShow = false;
                    })
                })
            },
            // 给职位添加用户
            postAddUserShow(item, item2) {
                this.postAddUser.deptId = item.deptId;
                this.postAddUser.postId = item2.postId;
                this.postAddUser.postName = item2.postName;
                this.postAddUser.uid = '';
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getAllUser',
                    success: (data => {
                        this.postAddUser.userList = data.data;
                        this.postAddUser.show = true;
                    })
                })
            },
            //
            postAddUserPost() {
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/addUserAuthority',
                    data: {
                        uid: this.postAddUser.uid, // 是   int 用户ID
                        postId: this.postAddUser.postId, //  是   int 职位ID
                        deptId: this.postAddUser.deptId, //  是   int 职位ID
                    },
                    success: (data => {
                        popup.miss({ content: '添加成功！' });
                        this.getInitData();
                        this.postAddUser.show = false;
                    })
                })
            },
            // 新增|修改部门
            // addDepartment(type,item){
            //     this.showPagepop=true;
            //     if(type=='add'){  //新增部门
            //         this.pagepopOptions.title='新增部门'
            //         this.pagepopOptions.titleh1='部门'
            //         this.bumenType='add'
            //     }else if(type=='update'){   //修改部门
            //         this.bumenType='update'
            //         this.pagepopOptions.title='修改部门'
            //         this.pagepopOptions.titleh1='部门'
            //         this.deptId=item.deptId
            //         this.causeofreezing=item.deptName
            //     }else if(type=='updatezhiwei'){  //修改职位
            //         this.bumenType='updatezhiwei'
            //         this.pagepopOptions.title='修改职位'
            //         this.pagepopOptions.titleh1='职位'
            //         this.postId=item.postId
            //         this.causeofreezing=item.postName
            //     }
            // },
            deptShow(type,item) {
                console.log(type)
                if (type === 'add') {
                    this.deptObj = {
                        show: true,
                        deptName: '',
                        deptId: '',
                    }
                } else {
                    this.deptObj = {
                        show: true,
                        deptName: item.deptName,
                        deptId: item.deptId,
                    }
                }
            },
            // 新增|修改部门 ajax
            addOrUpdateDept() {
                var api = '';
                if (this.deptObj.deptId === '') {  //新增部门
                    api = 'bookMall-admin/api/admin/addDept?deptName=' + this.deptObj.deptName
                } else{   //修改部门
                    api = 'bookMall-admin/api/admin/updateDeptName?deptId=' + this.deptObj.deptId + '&deptName=' + this.deptObj.deptName
                }
                util.ajax({
                    url: config.baseApi + api,
                    success:(data)=> {
                        this.getInitData();
                        popup.miss({ content: '操作成功!' });
                        this.deptObj.show = false;
                    }
                })
            },
            // 新增职位
            addDepartmentAjax(item) {
                var This = this;
                if (!util.regCombination('*').test(This.causezhiwei)) {
                    popup.alert({ content: '请填写职位!' }); return false;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/addPost',
                    data: {
                        deptId: item.deptId,
                        postName: This.causezhiwei
                    },
                    success: function (data) {
                        This.getInitData();
                        popup.miss({ content: '操作成功!' })
                    }
                })
            },
            // 权限复制
            copyUserShow(item) {
                this.copyUserObj.copyObjUid = item.uid;//源对象
                this.copyUserObj.copyObjName = item.userName;//源对象
                this.copyUserObj.beCopyObjUid = '';//目标对象
                this.copyUserObj.userInput = '';//搜索用
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/getAllUser',
                    success: (data => {
                        this.copyUserObj.userList = data.data;
                        this.copyUserObj.show = true;
                    })
                })
            },
            //
            copyUserBtn() {
                if (this.copyUserObj.beCopyObjUid == '') {
                    popup.alert({ content: '请选择需要复制权限的用户！' });
                    return;
                }
                util.ajax({
                    url: config.baseApi + 'bookMall-admin/api/admin/copyUserAccess',
                    data: {
                        beCopyObjUid: this.copyUserObj.beCopyObjUid, //目标对象
                        copyObjUid: this.copyUserObj.copyObjUid, //源对象
                    },
                    success: (data => {
                        popup.miss({ content: '复制成功！' });
                        this.copyUserObj.show = false;
                        this.getInitData();
                    })
                })
            },
            // 下拉
            nextSelect($event, $index, deptId, callback) {
                //如果$event没有target,那就是通过其他方法调的，如果有，就是点击调的
                var ulObj, e;
                if ($event.target) {
                    e = $event.target
                } else {
                    e = $event
                }
                ulObj = $(e).siblings('ul');
                this.getPostByDeptId($index, deptId, function () {
                    // 如果是别的方法调的，只展开不收回
                    if (!$event.target) {
                        ulObj.slideDown('fast');
                        $(e).html('&#xe65e;');
                        callback && callback();
                        return;
                    }
                    if (ulObj.css('display') == 'none') {
                        ulObj.slideDown('fast');
                        $(e).html('&#xe65e;');
                    } else {
                        ulObj.slideUp('fast');
                        $(e).html('&#xe65f;');
                    }
                    callback && callback()
                })
            },
            nextSelect2($event, index, index2, deptId, postId) {
                var ulObj, e;
                if ($event.target) {
                    e = $event.target
                } else {
                    e = $event
                }
                ulObj = $(e).siblings('ul');
                this.getUserByPostId(index, index2, deptId, postId, () => {
                    // 如果是别的方法调的，只展开不收回
                    if (!$event.target) {
                        ulObj.slideDown('fast');
                        $(e).html('&#xe65e;');
                        return;
                    }
                    if (ulObj.css('display') == 'none') {
                        ulObj.slideDown('fast');
                        $(e).html('&#xe65e;');
                    } else {
                        ulObj.slideUp('fast');
                        $(e).html('&#xe65f;');
                    }
                });
            },
            // 下拉新增
            addnextSelect($event) {
                var slibobj = $($event.target).siblings('span.add')
                if (slibobj.css('display') == 'none') {
                    $($event.target).siblings('ul').slideDown('fast');
                    $($event.target).siblings('span.first_span').html('&#xe65e;');
                    slibobj.show()
                } else {
                    slibobj.hide()
                }
            },
            // 删除部门
            delDept(item) {
                popup.confirm({
                    content: `是否删除部门（${item.deptName}）`,
                    yes: () => {
                        util.ajax({
                            url: config.baseApi + 'bookMall-admin/api/admin/deleteAuthority',
                            data: {
                                type: 'D',
                                deptId: item.deptId
                            },
                            success: (data => {
                                popup.miss({ content: '删除成功！' });
                                this.getInitData();
                            })
                        })
                    }
                })
            },
            // 删除职位
            delPost(item, item2) {
                popup.confirm({
                    content: `是否删除职位 ${item.deptName}-${item2.postName} `,
                    yes: () => {
                        util.ajax({
                            url: config.baseApi + 'bookMall-admin/api/admin/deleteAuthority',
                            data: {
                                type: 'P',
                                deptId: item.deptId,
                                postId: item2.postId
                            },
                            success: (data => {
                                popup.miss({ content: '删除成功！' });
                                this.getInitData();
                            })
                        })
                    }
                })
            },
            // 删除用户
            delUser(item, item2, item3) {
                popup.confirm({
                    content: `是否要删除用户 ${item.deptName}-${item2.postName}-${item3.userName}`,
                    yes: () => {
                        util.ajax({
                            url: config.baseApi + 'bookMall-admin/api/admin/deleteAuthority',
                            data: {
                                type: 'U',
                                deptId: item.deptId,
                                postId: item2.postId,
                                uid: item3.uid
                            },
                            success: (data => {
                                popup.miss({ content: '删除成功！' });
                                this.getInitData();
                            })
                        })
                    }
                })
            },
            //显示并获取用户权限
            getUserDeptAndPostList(item) {
                clearTimeout(this.timeout);
                this.timeout = setTimeout(() => {
                    util.ajax({
                        url: config.baseApi + 'bookMall-admin/api/admin/getUserDeptAndPostList?uid=' + item.uid,
                        noloading: true,
                        success: (data => {
                            item.show = true;
                            item.postList = data.data;
                        })
                    })
                }, 1000);
            },
            //隐藏用户权限
            closePostList(item) {
                item.show = false;
                clearTimeout(this.timeout);
            },
            deptSelectChange(val){
                let deptName = val[0].split('-')[0];
                let deptId = val[0].split('-')[1];
                let postName = val[1].split('-')[0];
                let postId = val[1].split('-')[1];
                this.addUser.deptJson = [
                    {
                        deptName:deptName,
                        deptId:deptId,
                        postName:postName,
                        postId:postId,
                    }
                ]
            },
            deptSelectChangeEdit(val) {
                let deptName = val[0].split('-')[0];
                let deptId = val[0].split('-')[1];
                let postName = val[1].split('-')[0];
                let postId = val[1].split('-')[1];
                this.addUser.deptJson = [
                    {
                        deptId: deptId,
                        postName: postName,
                        postId: postId,
                    }
                ]
            },
        }
    };
</script>